﻿using Company.Application.Share.Entities;
using Company.Application.Share.Interfaces;
using Company.Application.Share.Models;
using Company.Core.Models;
using Prism.Commands;
using Prism.Events;
using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;

namespace Company.Application.Login.ViewModels
{
    internal class LoginViewModel : BindableBase
    {
        private string hospitalName = string.Empty;
        public string HospitalName
        {
            get=> hospitalName;
            set => SetProperty(ref hospitalName, value);
        }
        private List<HospitalEntity> hospitalEntities;
        public List<HospitalEntity> HospitalEntities
        {
            get => hospitalEntities;
            set => SetProperty(ref hospitalEntities, value);
        }
        private HospitalEntity hospitalEntity;
        public HospitalEntity HospitalEntity
        {
            get => hospitalEntity;
            set => SetProperty(ref hospitalEntity, value);
        }
        private bool isOpenPopup = false;
        public bool IsOpenPopup
        {
            get => isOpenPopup;
            set => SetProperty(ref isOpenPopup, value);

        }

        public ICommand LoginCommand { get; }
        public ICommand SpaceKeyPressCommand { get; }
        public ICommand PreviewMouseUpCommand { get; }
        public ISession Session { get; }
        private IWebManager WebManager { get; }
        private IEventAggregator EventAggregator { get; }
        public LoginViewModel(ISession session, IWebManager webManager,
            IEventAggregator eventAggregator)
        {
            Session = session;
            WebManager = webManager;
            EventAggregator = eventAggregator;
            LoginCommand = new DelegateCommand(OnLoginCommand);
            SpaceKeyPressCommand = new DelegateCommand<string>(OnSpaceKeyPressCommand);
            PreviewMouseUpCommand = new DelegateCommand(OnPreviewMouseUpCommand);
        }

        private void OnPreviewMouseUpCommand()
        {
            IsOpenPopup = false;
            HospitalName = HospitalEntity.Name;
            Session.CurrentHospital = HospitalEntity;
#if DEBUG
            Session.CurrentUser = new UserEntity { Name="admin",Password="12345678"};
#endif
        }

        /// <summary>
        /// 按关键字搜索医院
        /// </summary>
        /// <param name="keyword"></param>
        private async void OnSpaceKeyPressCommand(string keyword)
        {
            if(!string.IsNullOrEmpty(keyword))
            {
                var result = await WebManager.GetHospitalsAsync<Result<List<HospitalEntity>>>(keyword);
                if (result != null && result.Success)
                {
                    HospitalEntities = result.Value;
                    IsOpenPopup= true;
                }
                else
                {
                    MessageBox.Show("没有相关记录");
                }
            }
        }

        /// <summary>
        /// 登录用户
        /// </summary>
        private async void OnLoginCommand()
        {
            var result = await WebManager.LoginAsync<Result<UserEntity>>(
                Session.CurrentUser.Name, 
                Session.CurrentUser.Password,
                Session.CurrentHospital.Id);
            if(result != null && result.Success)
            {
                //登录成功
                EventAggregator.GetEvent<LoginSuccessEvent>().Publish(result.Value);
            }
            else
            {
                MessageBox.Show("操作失败");
            }

        }
    }
}
